package com.twu.biblioteca;

import com.twu.model.Librarian;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;


public class Biblioteca {

    private final PrintStream out;

    public Biblioteca(PrintStream out) {

        this.out = out;
    }

    public static void main(String[] args) {

        OptionManager optionManager = new OptionManager();
        OptionInvoker optionInvoker = new OptionInvoker(optionManager);

        new Biblioteca(System.out).startApplication(optionInvoker, optionManager);

    }

    //Start Application
    public void startApplication(OptionInvoker optionInvoker, OptionManager optionManager){

        String custNumber, custPassword, valid, libPassword, mainMenu;
        int option;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        try{

            out.println(returnWelcomeMessage());

            mainMenu = MainMenu.getMainMenu();

            out.print(mainMenu);

            out.print("Enter option: ");
            option = Integer.parseInt(br.readLine());

            //Customer functionality
            switch(option) {

                case 1:

                    Customer customer = new Customer();

                    out.print("\nEnter customer number: ");
                    custNumber = br.readLine();
                    out.print("\nEnter customer password: ");
                    custPassword = br.readLine();

                    valid = customer.login(custNumber, custPassword);

                    out.print("\n" + valid);

                    if (valid.equals("Successful login")) {
                        runCustomerFunctions(customer, optionInvoker, optionManager);
                    } else {
                        startApplication(optionInvoker, optionManager);
                    }

                    break;


                case 2:

                    Librarian l = new Librarian();
                    out.print("\nEnter librarian password: ");
                    libPassword = br.readLine();

                    valid = l.login(libPassword, optionManager);

                    if(valid.equals("Success")){
                        displayGoBackToMainMenuOption(optionInvoker, optionManager);

                    }
                    else{
                        out.print("Enter valid option!");
                        startApplication(optionInvoker, optionManager);
                    }

                case 3:

                    System.exit(0);

                default:

                    out.print("\nEnter a valid option!");
                    startApplication(optionInvoker, optionManager);
            }

        }catch(Exception e){
            e.printStackTrace();
        }
    }


    //Go back for main menu (login menu)
    private void displayGoBackToMainMenuOption(OptionInvoker optionInvoker,OptionManager optionManager) {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String input;

        try{
            out.print("\n\nGo (b)ack to Main Menu : ");
            input = br.readLine();
            if (input.equals("b")) {
                startApplication(optionInvoker, optionManager);
            } else {
                out.println("Enter valid option!");
                displayGoBackToMainMenuOption(optionInvoker, optionManager);
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    //Display Welcome Message
    public String returnWelcomeMessage(){

        return "\n\n\n\t\t\t\t\t\tWelcome to BIBLIOTECA!";

    }

    //Repeat options
    private void displayGoBackOption(Customer c, OptionInvoker optionInvoker, OptionManager optionManager){

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String input;

        try{
            out.print("\n\nGo (b)ack to Main Menu : ");
            input = br.readLine();
            if (input.equals("b")) {
                runCustomerFunctions(c, optionInvoker, optionManager);
            } else {
                out.println("Enter valid option!");
                displayGoBackOption(c, optionInvoker, optionManager);
            }
        }catch(Exception e){
            e.printStackTrace();
        }


    }

    public void runCustomerFunctions(Customer customer, OptionInvoker optionInvoker, OptionManager optionManager) {

        String custMenu;

        out.println(returnWelcomeMessage());

        custMenu = CustomerMenu.getMenu();

        out.print(custMenu);

        out.print("\nEnter you choice: ");

        try{
            BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in));
            int option = Integer.parseInt(bufferRead.readLine());

            if(option==8) {
                startApplication(optionInvoker, optionManager);
            }

            CustomerMenu customerMenu = CustomerMenu.values()[option-1];
            out.print(optionInvoker.invoke(customerMenu, customer));
            displayGoBackOption(customer, optionInvoker, optionManager);

            if(!(option<8) && !(option>0)) {
                out.println("Select a valid option!");
                runCustomerFunctions(customer, optionInvoker, optionManager);
            }

        }catch(Exception e){
            //e.printStackTrace();
        }
    }

}
